// ignore_for_file: must_be_immutable

import 'package:aitao_flutter/res/r.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../utils/sp_utils.dart';

class CircleAvatarWidget extends StatefulWidget {
  CircleAvatarWidget(
      {Key? key,
      required this.url,
      this.imgWidth = 48,
      this.imgHeight = 48,
      required this.gender})
      : super(key: key);
  final String url;

  // 1:女 2:男 4:系统消息
  int gender;
  double imgWidth;
  double imgHeight;
  @override
  State<CircleAvatarWidget> createState() => _CircleAvatarWidgetState();
}

class _CircleAvatarWidgetState extends State<CircleAvatarWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.url == ''
        ? Image.asset(
            widget.gender == 1
                ? ImagePath.assetsImagesHeaderForWomen
                : ImagePath.assetsImagesHeaderForMan,
            width: widget.imgWidth.w,
            height: widget.imgHeight.w,
            fit: BoxFit.fill,
          )
        : ClipOval(
            // child: CachedNetworkImage(
            //   imageUrl: SpUtil.getImageUrl(url),
            //   width: imgWidth.w,
            //   height: imgHeight.w,
            //   fit: BoxFit.cover,
            //   errorWidget: (context, err, obj) {
            //     return Image.asset(
            //       gender == 1
            //           ? ImagePath.assetsImagesHeaderForWomen
            //           : ImagePath.assetsImagesHeaderForMan,
            //       width: imgWidth.w,
            //       height: imgHeight.w,
            //       fit: BoxFit.cover,
            //     );
            //   },
            // ),
            child: ExtendedImage.network(
              SpUtil.getImageUrl(widget.url),
              width: widget.imgWidth.w,
              height: widget.imgHeight.w,
              fit: BoxFit.fill,
              cache: true,
              loadStateChanged: (ExtendedImageState state) {
                switch (state.extendedImageLoadState) {
                  case LoadState.completed:
                    return ExtendedRawImage(
                      image: state.extendedImageInfo?.image,
                      width: widget.imgWidth.w,
                      height: widget.imgHeight.w,
                      fit: BoxFit.fill,
                    );
                  default:
                    return Image.asset(
                      widget.gender == 1
                          ? ImagePath.assetsImagesHeaderForWomen
                          : ImagePath.assetsImagesHeaderForMan,
                      width: widget.imgWidth.w,
                      height: widget.imgHeight.w,
                      fit: BoxFit.fill,
                    );
                }
              },
            ),
          );
  }
}
